<template>
	<div class="padding20">
		<el-row style="margin-bottom: 7px">
			<el-form v-if="searchController" :inline="true" :model="queryForm" @submit.native.prevent @keyup.enter.native="queryList">
				<el-form-item>
					<el-input prefix-icon="el-icon-search" v-model="queryForm.userName" autocomplete="off" placeholder="请输入用户名" clearable></el-input>
				</el-form-item>

				<el-form-item>
					<el-input prefix-icon="el-icon-search" v-model="queryForm.phone" autocomplete="off" placeholder="请输入手机号" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.nickName" autocomplete="off" placeholder="请输入昵称" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.cardNumber" autocomplete="off" placeholder="请输入会员卡号" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.openid" autocomplete="off" placeholder="请输入微信openid" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.unionid" autocomplete="off" placeholder="请输入微信unionid" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<fortress-select v-model="queryForm.gender" :dictList="sexList" placeholder="请选择性别" clearable filterable></fortress-select>
				</el-form-item>
				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.province" autocomplete="off" placeholder="请输入省" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.provinceName" autocomplete="off" placeholder="请输入省名称" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.city" autocomplete="off" placeholder="请输入市" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.cityName" autocomplete="off" placeholder="请输入市名称" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.area" autocomplete="off" placeholder="请输入区" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.areaName" autocomplete="off" placeholder="请输入区名称" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<fortress-select v-model="queryForm.source" :dictList="sourceList" placeholder="请选择会员来源" clearable filterable></fortress-select>
				</el-form-item>
				<el-form-item v-show="collapse.flag">
					<el-input prefix-icon="el-icon-search" v-model="queryForm.invitation" autocomplete="off" placeholder="请输入邀请人" clearable></el-input>
				</el-form-item>

				<el-form-item v-show="collapse.flag">
					<fortress-select v-model="queryForm.levelId" :dictList="levelIdList" placeholder="请选择会员等级id" clearable filterable></fortress-select>
				</el-form-item>
				<el-form-item v-show="collapse.flag">
					<fortress-select v-model="queryForm.status" :dictList="MemberStatusList" placeholder="请选择会员状态(正常、禁用、黑名单)" clearable filterable></fortress-select>
				</el-form-item>
				<el-form-item v-show="collapse.flag">
					<fortress-select v-model="queryForm.auditStatus" :dictList="YesNoList" placeholder="请选择审核状态（是否实名）" clearable filterable></fortress-select>
				</el-form-item>
				<el-form-item v-show="collapse.flag">
					<fortress-select v-model="queryForm.realType" :dictList="MemberRealTypeList" placeholder="请选择认证类型（实名类型）" clearable filterable></fortress-select>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="medium" icon="el-icon-search" @click="queryList({ current: 1 })">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="medium" icon="el-icon-refresh-left" @click="resetQueryForm">重置</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="text" @click="handleCollapseState">
						{{ collapse.label }}
						<i :class="collapse.icon"></i>
					</el-button>
				</el-form-item>
			</el-form>
			<el-button type="primary" size="medium" icon="el-icon-plus" v-if="$tools.hasPermission('member:member:save')" @click="handleEdit()">
				添加
			</el-button>
			<el-button v-if="$tools.hasPermission('member:member:deleteBatch')" size="medium" type="danger" icon="el-icon-delete" @click="handleDeleteBatch()">
				删除
			</el-button>
			<div style="float: right; padding-right: 17px">
				<el-tooltip class="item" effect="dark" :content="searchTips" placement="top">
					<i class="el-icon-search" style="margin-right: 10px; cursor: pointer" @click="handlesearch"></i>
				</el-tooltip>
				<el-tooltip class="item" effect="dark" content="导出文件" placement="top">
					<i class="el-icon-download" style="margin-right: 10px; cursor: pointer" @click="handleExport"></i>
				</el-tooltip>
				<el-tooltip class="item" effect="dark" content="刷新" placement="top">
					<i class="el-icon-refresh" style="margin-right: 10px; cursor: pointer" @click="refresh"></i>
				</el-tooltip>
				<el-tooltip class="item" effect="dark" content="列展示" placement="top">
					<el-popover placement="bottom" width="180" trigger="click" title="列展示" popper-class="columnShows">
						<el-checkbox-group v-model="tableShowList">
							<el-checkbox v-for="item in tableList" :label="item.propName" :key="item.propName" :value="item.propName">
								{{ item.propDesc }}
							</el-checkbox>
						</el-checkbox-group>
						<i slot="reference" class="el-icon-s-operation" style="cursor: pointer"></i>
					</el-popover>
				</el-tooltip>
			</div>
		</el-row>
		<el-table ref="multipleTable" :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="id" border>
			<el-table-column type="selection" width="55" show-overflow-tooltip></el-table-column>

			<el-table-column v-if="$tools.isShow('userName', tableShowList)" prop="userName" label="用户名" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('password', tableShowList)" prop="password" label="密码" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('phone', tableShowList)" prop="phone" label="手机号" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('nickName', tableShowList)" prop="nickName" label="昵称" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('cardNumber', tableShowList)" prop="cardNumber" label="会员卡号" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('openid', tableShowList)" prop="openid" label="微信openid" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('unionid', tableShowList)" prop="unionid" label="微信unionid" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('avatar', tableShowList)" prop="avatar" label="头像" :show-overflow-tooltip="true">
				<template slot-scope="scope">
					<el-image v-if="scope.row.avatar" :src="scope.row.avatar" style="width: 100px; height: 100px" :preview-src-list="[scope.row.avatar]" />
				</template>
			</el-table-column>
			<el-table-column v-if="$tools.isShow('gender', tableShowList)" prop="gender" label="性别" :show-overflow-tooltip="true" :formatter="genderFmt"></el-table-column>
			<el-table-column v-if="$tools.isShow('birthday', tableShowList)" prop="birthday" label="生日" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('province', tableShowList)" prop="province" label="省" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('provinceName', tableShowList)" prop="provinceName" label="省名称" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('city', tableShowList)" prop="city" label="市" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('cityName', tableShowList)" prop="cityName" label="市名称" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('area', tableShowList)" prop="area" label="区" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('areaName', tableShowList)" prop="areaName" label="区名称" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('source', tableShowList)" prop="source" label="会员来源" :show-overflow-tooltip="true" :formatter="sourceFmt"></el-table-column>
			<el-table-column v-if="$tools.isShow('invitation', tableShowList)" prop="invitation" label="邀请人" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column v-if="$tools.isShow('levelId', tableShowList)" prop="levelId" label="会员等级id" :show-overflow-tooltip="true" :formatter="levelIdFmt"></el-table-column>
			<el-table-column v-if="$tools.isShow('status', tableShowList)" prop="status" label="会员状态(正常、禁用、黑名单)" :show-overflow-tooltip="true" :formatter="statusFmt"></el-table-column>
			<el-table-column v-if="$tools.isShow('auditStatus', tableShowList)" prop="auditStatus" label="审核状态（是否实名）" :show-overflow-tooltip="true" :formatter="auditStatusFmt"></el-table-column>
			<el-table-column v-if="$tools.isShow('realType', tableShowList)" prop="realType" label="认证类型（实名类型）" :show-overflow-tooltip="true" :formatter="realTypeFmt"></el-table-column>
			<el-table-column v-if="$tools.isShow('remark', tableShowList)" prop="remark" label="备注" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="操作" fixed="right" min-width="180">
				<template slot-scope="scope">
					<el-button v-if="$tools.hasPermission('member:member:info')" size="mini" plain @click="handleInfo(scope.row.id)">
						详情
					</el-button>
					<el-button v-if="$tools.hasPermission('member:member:edit')" size="mini" plain @click="handleEdit(scope.row.id)">
						编辑
					</el-button>
					<el-button v-if="$tools.hasPermission('member:member:delete')" size="mini" plain type="danger" @click="handleDelete(scope.row)">
						删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination :total="page.total" :current-page="page.current" :page-size="page.size" @refreshData="queryList"></pagination>
		<!-- 新增、编辑 -->
		<form-edit v-if="formEditVisible" ref="formEdit" @refreshData="queryList"></form-edit>
		<!-- 详情 -->
		<form-info v-if="formInfoVisible" ref="formInfo"></form-info>
	</div>
</template>
<script>
import { memberPage, memberDel, memberDelBatch, memberDynamicExport } from "@/api/member/member.js"
import { viewTableList } from "@/api/public.api.js"
import FormEdit from "./member-edit"
import FormInfo from "./member-info"
import FortressPagination from "@/components/fortress-pagination.vue"
import { dictList, enumList, urlList } from "@/api/public.api.js"
export default {
	data() {
		return {
			tableData: [],
			formEditVisible: false,
			formInfoVisible: false,
			queryForm: {
				userName: "",
				phone: "",
				nickName: "",
				cardNumber: "",
				openid: "",
				unionid: "",
				gender: "",
				province: "",
				provinceName: "",
				city: "",
				cityName: "",
				area: "",
				areaName: "",
				source: "",
				invitation: "",
				levelId: "",
				status: "",
				auditStatus: "",
				realType: "",
			},
			page: {
				current: 1,
				size: 10,
				total: 0,
			},
			collapse: {
				icon: "el-icon-arrow-down el-icon--right",
				label: "展开",
				flag: false,
			},
			tableList: [],
			tableShowList: [],
			sexList: [],
			sourceList: [],
			MemberStatusList: [],
			YesNoList: [],
			MemberRealTypeList: [],
			levelIdList: [],
			searchTips: "显示搜索",
			searchController: false, //搜索显示控制
		}
	},
	components: {
		"form-edit": FormEdit,
		"form-info": FormInfo,
		pagination: FortressPagination,
	},
	created() {
		this.queryDictList()
		this.queryEnumList()
		this.queryUrlList()
		this.initTableList()
		this.queryList()
	},
	methods: {
		//搜索控制
		handlesearch() {
			this.searchController = !this.searchController
			if (this.searchController) {
				this.searchTips = "隐藏搜索"
			} else {
				this.searchTips = "显示搜索"
			}
		},
		queryDictList() {
			this.sexList = []
			dictList("sex").then(resp => {
				if (resp.code == 0) {
					this.sexList = resp.data
				}
			})
			this.sourceList = []
			dictList("source").then(resp => {
				if (resp.code == 0) {
					this.sourceList = resp.data
				}
			})
		},
		queryEnumList() {
			this.MemberStatusList = []
			enumList("MemberStatus", "").then(resp => {
				if (resp.code == 0) {
					this.MemberStatusList = resp.data
				}
			})
			this.YesNoList = []
			enumList("YesNo", "").then(resp => {
				if (resp.code == 0) {
					this.YesNoList = resp.data
				}
			})
			this.MemberRealTypeList = []
			enumList("MemberRealType", "").then(resp => {
				if (resp.code == 0) {
					this.MemberRealTypeList = resp.data
				}
			})
		},
		queryUrlList() {
			this.levelIdList = []
			urlList("/member/memberVipLeve/list").then(resp => {
				if (resp.code == 0) {
					if (resp.data) {
						resp.data.forEach(item => {
							this.levelIdList.push({ code: item.id, name: item.name })
						})
					}
				}
			})
		},
		refresh() {
			this.tableData = []
			this.queryList({ current: 1 })
		},
		//查询表格数据
		queryList(page) {
			if (page) {
				this.page.current = page.current ? page.current : this.page.current
				this.page.size = page.size ? page.size : this.page.size
			}
			this.queryForm.current = this.page.current
			this.queryForm.size = this.page.size
			memberPage(this.queryForm).then(resp => {
				if (resp.code == 0) {
					this.tableData = resp.data.records
					this.page.total = resp.data.total
					//查询页大于总页数，重新查询
					let cu = this.page.total % this.page.size == 0 ? parseInt(this.page.total / this.page.size) : parseInt(this.page.total / this.page.size + 1)
					if (cu > 0 && this.page.current > cu) {
						this.queryList({ current: cu })
					}
				} else {
					this.$msg({
						message: resp.msg,
						type: "error",
					})
				}
			})
		},
		//打开编辑窗口
		handleEdit(id) {
			this.formEditVisible = true
			this.$nextTick(() => {
				this.$refs.formEdit.init(id)
			})
		},
		//打开详情窗口
		handleInfo(id) {
			this.formInfoVisible = true
			this.$nextTick(() => {
				this.$refs.formInfo.init(id)
			})
		},
		//删除
		handleDelete(row) {
			this.$confirm("确认删除该记录, 是否继续?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning",
			})
				.then(() => {
					memberDel(row.id).then(resp => {
						if (resp.code == 0) {
							this.$message({
								type: "success",
								message: "操作成功!",
							})
							//判断是否最后一页的最后一条记录
							let totalPages = (this.page.total - 1) % this.page.size == 0 ? (this.page.total - 1) / this.page.size : (this.page.total - 1) / this.page.size + 1
							if (this.page.current > totalPages && this.page.current != 1) {
								this.page.current = this.page.current - 1
							}
							this.queryList()
						} else {
							this.$msg({
								message: resp.msg,
								type: "error",
							})
						}
					})
				})
				.catch(() => {})
		},
		//批量删除
		handleDeleteBatch() {
			let checkedRow = this.$refs.multipleTable.selection
			if (checkedRow.length > 0) {
				let ids = []
				checkedRow.forEach(item => {
					ids.push(item.id)
				})
				this.$confirm("确认删除该记录, 是否继续?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
				})
					.then(() => {
						memberDelBatch(ids).then(resp => {
							if (resp.code == 0) {
								this.$message({
									type: "success",
									message: "操作成功!",
								})
								//判断是否最后一页的最后一条记录
								let totalPages = (this.page.total - checkedRow.length) % this.page.size == 0 ? (this.page.total - checkedRow.length) / this.page.size : (this.page.total - checkedRow.length) / this.page.size + 1
								if (this.page.current > totalPages && this.page.current != 1) {
									this.page.current = this.page.current - 1
								}
								this.queryList()
							} else {
								this.$msg({
									message: resp.msg,
									type: "error",
								})
							}
						})
					})
					.catch(() => {})
			} else {
				this.$msg({
					message: "请选择数据进行删除",
					type: "error",
				})
			}
		},
		//重置查询框
		resetQueryForm() {
			this.queryForm = {
				userName: "",
				phone: "",
				nickName: "",
				cardNumber: "",
				openid: "",
				unionid: "",
				gender: "",
				province: "",
				provinceName: "",
				city: "",
				cityName: "",
				area: "",
				areaName: "",
				source: "",
				invitation: "",
				levelId: "",
				status: "",
				auditStatus: "",
				realType: "",
			}
			this.queryList({ current: 1 })
		},
		//切换查询条件收缩框
		handleCollapseState() {
			this.collapse.flag = !this.collapse.flag
			if (this.collapse.flag) {
				this.collapse.icon = "el-icon-arrow-up el-icon--right"
				this.collapse.label = "收起"
			} else {
				this.collapse.icon = "el-icon-arrow-down el-icon--right"
				this.collapse.label = "展开"
			}
		},
		//视图表格列表初始化
		initTableList() {
			viewTableList("member").then(resp => {
				if (resp.code == 0) {
					this.tableList = resp.data
					this.tableList.forEach(item => {
						this.tableShowList.push(item.propName)
					})
				}
			})
		},
		//性别格式化返回
		genderFmt(row) {
			let name = ""
			if (this.sexList) {
				this.sexList.forEach(item => {
					if (item.code == row.gender) {
						name = item.name
					}
				})
			}
			return name
		},
		//会员来源格式化返回
		sourceFmt(row) {
			let name = ""
			if (this.sourceList) {
				this.sourceList.forEach(item => {
					if (item.code == row.source) {
						name = item.name
					}
				})
			}
			return name
		},
		//会员等级id格式化返回
		levelIdFmt(row) {
			let name = ""
			if (this.levelIdList) {
				this.levelIdList.forEach(item => {
					if (item.code == row.levelId) {
						name = item.name
					}
				})
			}
			return name
		},
		//会员状态(正常、禁用、黑名单)格式化返回
		statusFmt(row) {
			let name = ""
			if (this.MemberStatusList) {
				this.MemberStatusList.forEach(item => {
					if (item.code == row.status) {
						name = item.name
					}
				})
			}
			return name
		},
		//审核状态（是否实名）格式化返回
		auditStatusFmt(row) {
			let name = ""
			if (this.YesNoList) {
				this.YesNoList.forEach(item => {
					if (item.code == row.auditStatus) {
						name = item.name
					}
				})
			}
			return name
		},
		//认证类型（实名类型）格式化返回
		realTypeFmt(row) {
			let name = ""
			if (this.MemberRealTypeList) {
				this.MemberRealTypeList.forEach(item => {
					if (item.code == row.realType) {
						name = item.name
					}
				})
			}
			return name
		},
		//可见字段excel导出
		handleExport() {
			if (!this.tableShowList || this.tableShowList.length < 1) {
				this.$msg({
					message: "请选择要导出的列",
					type: "error",
				})
				return
			}
			let colNum = 1
			let headerList = []
			this.tableShowList.forEach(item => {
				let nameData = this.tableList.find(item2 => {
					return item2.propName == item
				})
				let name = ""
				if (nameData && nameData.propDesc) {
					name = nameData.propDesc
				} else {
					name = "未定义字段" + colNum++
				}
				headerList.push({ name: name, code: item })
			})
			memberDynamicExport({ headerList: headerList, dto: this.queryForm })
		},
	},
}
</script>
<style scoped></style>
